<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上载演示</title>
    <script src="bower_components/jquery/dist/jquery.min.js"></script>
    <!--引入vue-->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <!--引入axion-->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
<form id="demoForm" method="post"
      enctype="multipart/form-data"
      action="/upload/file" >
    <div>
        <label>上传文件
            <input id="imageFile" type="file" name="imageFile">
        </label>
    </div>
    <button type="submit">上传文件</button>
</form>
<img id="image" src=""  alt="">
</body>
<script>
    $("#demoForm").submit(function () {
        let files = document.getElementById("imageFile").files;
        if (files.length>0){
            uploadFile(files[0])
        }else {
            alert("选文件啊,傻逼")
        }
        //阻止提交 等效 @click.prevent
        return false;
    })
    //上传文件的方法
    function uploadFile(file) {
        let form  = new FormData();
        form.append("imageFile",file);
        axios.post("/upload/file",form).then(function (response) {
            console.log(response.data)
            //将上传的图片路径赋值给img标签的SRC属性
            $("#image").attr("src",response.data)
        })
    }
</script>
</html>